package com.tulele.vtool.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tulele.vtool.mapper.BargainParticipantsInfoMapper;
import com.tulele.vtool.mapper.PlatformActivityInfoMapper;
import com.tulele.vtool.mapper.UserBargainActivityInfoMapper;
import com.tulele.vtool.model.BargainParticipantsInfo;
import com.tulele.vtool.model.PlatformActivityInfo;
import com.tulele.vtool.model.UserBargainActivityInfo;
import com.tulele.vtool.model.UserInfo;
import com.tulele.vtool.service.IUserBargainActivityService;
import com.tulele.vtool.service.IUserInfoService;
import com.tulele.vtool.utils.DateUtil;
import com.tulele.vtool.utils.SysConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserBargainActivityServiceImpl implements IUserBargainActivityService {

    @Autowired
    private PlatformActivityInfoMapper platformActivityInfoMapper;

    @Autowired
    private UserBargainActivityInfoMapper userBargainActivityInfoMapper;

    @Autowired
    private BargainParticipantsInfoMapper bargainParticipantsInfoMapper;

    @Autowired
    private IUserInfoService userInfoService;

    @Override
    public PlatformActivityInfo getPlatformBargainActivityInfo(Integer actType) {
        return platformActivityInfoMapper.queryPlatformActivityInfoByType(actType);
    }

    @Override
    public int getUserParticipateActStatus(Integer userId, Integer bargainActId) {
        return platformActivityInfoMapper.queryUserParticipateActStatus(userId, bargainActId);
    }

    @Override
    public int participateBargainActivity(int actInitUserId, int participateUserId, int bargainActId, int bargainAmount) {

        int finalActId = bargainActId;
        if ((actInitUserId == participateUserId) && bargainActId == 0) {

            UserBargainActivityInfo userBargainActivityInfo = new UserBargainActivityInfo();
            userBargainActivityInfo.setUserId(participateUserId);
            userBargainActivityInfo.setBargainTotalAmount(bargainAmount);
            userBargainActivityInfo.setEffectivePeriod(SysConstant.BARGAIN_ACTIVITY_EFFECTIVE_PERIOD);
            userBargainActivityInfo.setActStatus(1);
            userBargainActivityInfo.setParticipantsTotalCount(1);
            userBargainActivityInfo.setCreateTime(DateUtil.getCurDate());

            userBargainActivityInfoMapper.addUserBargainActivityInfo(userBargainActivityInfo);

            finalActId = userBargainActivityInfo.getId();

        }

        //添加参与记录
        BargainParticipantsInfo bargainParticipantsInfo = new BargainParticipantsInfo();
        bargainParticipantsInfo.setUserId(participateUserId);
        bargainParticipantsInfo.setBargainActId(finalActId);
        bargainParticipantsInfo.setBargainAmount(bargainAmount);

        UserInfo userInfo = userInfoService.getUserBaseInfo(participateUserId);

        bargainParticipantsInfo.setNickName(userInfo.getNickName());
        bargainParticipantsInfo.setAvatarUrl(userInfo.getAvatarUrl());

        bargainParticipantsInfo.setCreateTime(DateUtil.getCurDate());

        bargainParticipantsInfoMapper.addBargainParticipantsInfo(bargainParticipantsInfo);

        return finalActId;
    }

    @Override
    public UserBargainActivityInfo getUserBargainActInfo(Integer bargainActId) {
        return userBargainActivityInfoMapper.queryUserBargainActInfo(bargainActId);
    }

    @Override
    public Integer getUserBargainAmountByActId(Integer userId, Integer bargainActId) {
        return bargainParticipantsInfoMapper.queryUserBargainAmountByActId(userId, bargainActId);
    }

    @Override
    public List<BargainParticipantsInfo> getSpecialSizeBargainParticipantsInfoByActId(Integer bargainActId) {
        return bargainParticipantsInfoMapper.querySpecialSizeBargainParticipantsInfoByActId(bargainActId);
    }

    @Override
    public PageInfo<BargainParticipantsInfo> getBargainParticipantsInfoByActId(Integer bargainActId, int pageNum, int pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<BargainParticipantsInfo> bargainParticipantsInfoList = bargainParticipantsInfoMapper.queryBargainParticipantsInfoByActId(bargainActId);

        return new PageInfo<BargainParticipantsInfo>(bargainParticipantsInfoList);
    }


}
